package com.kcyu.sfhinvoice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kcyu.sfhinvoice.domain.Invoice;
import com.kcyu.sfhinvoice.dto.StatisticRankVO;
import com.kcyu.sfhinvoice.dto.StatisticVO;
import com.kcyu.sfhinvoice.mapper.CommonTemplateMapper;
import com.kcyu.sfhinvoice.mapper.InvoiceMapper;
import com.kcyu.sfhinvoice.mapper.UserMapper;
import com.kcyu.sfhinvoice.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommonTemplateMapper commonTemplateMapper;

    @Override
    public StatisticVO getStatistic() {
        StatisticVO statisticVO = new StatisticVO();
        // 总发票数和总金额
        QueryWrapper<Invoice> select = new QueryWrapper<Invoice>().select("sum(invoice_count) as amount", "count(*) as count");
        List<Map<String, Object>> maps = invoiceMapper.selectMaps(select);
        statisticVO.setTotalInvoiceCount(Integer.valueOf(maps.get(0).get("count").toString()));
        statisticVO.setTotalInvoiceAmount(new BigDecimal(maps.get(0).get("amount").toString()));
        // 未开发票数和已开发票数
        QueryWrapper<Invoice> select1 = new QueryWrapper<Invoice>().select("count(*) as count").eq("status", "INIT");
        List<Map<String, Object>> maps1 = invoiceMapper.selectMaps(select1);
        statisticVO.setTotalInvoiceAmountUnopened(Integer.valueOf(maps1.get(0).get("count").toString()));
        QueryWrapper<Invoice> select2 = new QueryWrapper<Invoice>()
                .select("count(*) as count")
                .eq("status", "ADDED").or()
                .eq("status", "FINISHED");
        List<Map<String, Object>> maps2 = invoiceMapper.selectMaps(select2);
        statisticVO.setTotalInvoiceAmountOpened(Integer.valueOf(maps2.get(0).get("count").toString()));
        // 顺丰开票数、合丰开票数
        QueryWrapper<Invoice> select3 = new QueryWrapper<Invoice>().select("count(*) as count").eq("seller", "顺丰");
        List<Map<String, Object>> maps3 = invoiceMapper.selectMaps(select3);
        statisticVO.setSfInvoiceCount(Integer.valueOf(maps3.get(0).get("count").toString()));
        QueryWrapper<Invoice> select4 = new QueryWrapper<Invoice>().select("count(*) as count").eq("seller", "合丰");
        List<Map<String, Object>> maps4 = invoiceMapper.selectMaps(select4);
        statisticVO.setHfInvoiceCount(Integer.valueOf(maps4.get(0).get("count").toString()));

        // 开票数量排行榜
        List<Map<String, Object>> top10 = invoiceMapper.selectMaps(new QueryWrapper<Invoice>().select("buyer", "count(*) as count").groupBy("buyer").orderByDesc("count(*)").last("limit 10"));
        List<StatisticRankVO> statisticRankVOS = new ArrayList<>();
        for (Map<String, Object> map : top10) {
            StatisticRankVO statisticRankVO = new StatisticRankVO();
            statisticRankVO.setCustomerName(map.get("buyer").toString());
            statisticRankVO.setInvoiceCount(Integer.valueOf(map.get("count").toString()));
            statisticRankVOS.add(statisticRankVO);
        }
        statisticVO.setInvoiceCountRanking(statisticRankVOS);

        // 开票金额排行榜
        List<Map<String, Object>> top10Amount = invoiceMapper.selectMaps(new QueryWrapper<Invoice>().select("buyer", "sum(invoice_count) as amount").groupBy("buyer").orderByDesc("amount").last("limit 10"));
        List<StatisticRankVO> statisticRankVOSAmount = new ArrayList<>();
        for (Map<String, Object> map : top10Amount) {
            StatisticRankVO statisticRankVO = new StatisticRankVO();
            statisticRankVO.setCustomerName(map.get("buyer").toString());
            statisticRankVO.setInvoiceAmount(new BigDecimal(map.get("amount").toString()));
            statisticRankVOSAmount.add(statisticRankVO);
        }
        statisticVO.setInvoiceAmountRanking(statisticRankVOSAmount);

        // 用户数
        Long l = userMapper.selectCount(new QueryWrapper<>());
        statisticVO.setUserCount(l.intValue());
        // 模板数
        Long l1 = commonTemplateMapper.selectCount(new QueryWrapper<>());
        statisticVO.setTemplateCount(l1.intValue());
        return statisticVO;
    }
}
